<?php

/**
 * Description of ImageFunctions
 *
 * @author a.novikov
 */
class ImageFunctions {

	public static $quality = 90;

	public static function factory($filename) {
		
	}


	public static function fix_image($filename, $dstFilename, $max_width, $max_height, $del_spaces = false) {

		$type = 0;
		$info = getimagesize($filename);
		switch ($info[2]) {
			case 2:
				$image = @imagecreatefromjpeg($filename);
				$type = 'jpg';
				break;
			case 3:
				$image = @imagecreatefrompng($filename);
				$type = 'png';
				break;
			case 1:
				$image = @imagecreatefromgif($filename);
				$type = 'gif';
				break;
			default:
				throw new Exception('Unsupported image type');
		}
		
		if ($del_spaces) {
			$image = self::del_spaces($image);
		}

		$image_new = self::fix_image_resource($image, $max_width, $max_height);
		return imagejpeg($image_new, $dstFilename, self::$quality);
//		switch ($type) {
//			case 'jpg': imagejpeg($image_new, $dst_filename, self::$quality); break;
//			case 'png': imagepng($image_new, $dst_filename); break;
//			case 'gif': imagegif($image_new, $dst_filename); break;
//		}
	}

	public static function watermark($img_src, $dst_filename) {

		$pct = 100;
		$warermark_src = BASE_DIR.'/htdocs/images/watermark_10px.png';

		
		$info = getimagesize($img_src);
		switch ($info[2]) {
			case 2:
				$image = @imagecreatefromjpeg($img_src);
				break;
			case 3:
				$image = @imagecreatefrompng($img_src);
				break;
			case 1:
				$image = @imagecreatefromgif($img_src);
				break;
			default:
				throw new Exception('Unsupported image type');
		}
		
		//$image = imagecreatefromjpeg($img_src);
		//$width = imagesx($image);
		//$height = imagesy($image);
		$width = $info[0];
		$height = $info[1];
		
		$watermark = self::fix_image_resource(imagecreatefrompng($warermark_src), $width / 2, $height / 2);

		$watermark_width = imagesx($watermark);
		$watermark_height = imagesy($watermark);
		$dest_x = $width - $watermark_width - 5;
		$dest_y = $height - $watermark_height - 5;

		self::imagecopymerge_alpha($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $pct);
		imagejpeg($image, $dst_filename, self::$quality);
	}

	public static function fix_image_resource($image, $max_width, $max_height) {
		$width = imagesx($image);
		$height = imagesy($image);

		$k = max($width / $max_width, $height / $max_height, 1);

		if ($k == 1) {
			return $image;
		}

		$new_width = (int)($width / $k);
		$new_height = (int)($height / $k);

		$image_new = imagecreatetruecolor($new_width, $new_height);

		imagecolortransparent($image_new, imagecolorallocate($image_new, 0, 0, 0));
		imagealphablending($image_new, false);
		imagesavealpha($image_new, true);

		imagecopyresampled($image_new, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

		return $image_new;
	}

	
    /**
     * PNG ALPHA CHANNEL SUPPORT for imagecopymerge();
     * This is a function like imagecopymerge but it handle alpha channel well!!!
     **/
	function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
		// getting the watermark width
		$w = imagesx($src_im);
		// getting the watermark height
		$h = imagesy($src_im);

		// creating a cut resource
		$cut = imagecreatetruecolor($src_w, $src_h);
		// copying that section of the background to the cut
		imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);

		// placing the watermark now
		imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
		imagecopymerge($dst_im, $cut, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct);
	}

	private static function del_spaces($image)
	{
		$width = imagesx($image);
		$height = imagesy($image);

		for ($i=0; $i<$height; $i++)
		{
			if ($start_y!==null)
				break;
			for ($j=0; $j<$width; $j++)
			{
				$pos = imagecolorat($image, $j, $i).' ';
				$f = imagecolorsforindex($image, $pos);
				if ($f['red']<240 || $f['green']<240 || $f['blue']<240)
				{
					$start_y = $i;
					break;
				}
			}
		}

		for ($j=0; $j<$width; $j++)
		{
			if ($start_x!==null)
				break;
			for ($i=0; $i<$height; $i++)
			{
				$pos = imagecolorat($image, $j, $i).' ';
				$f = imagecolorsforindex($image, $pos);
				if ($f['red']<240 || $f['green']<240 || $f['blue']<240)
				{
					$start_x = $j;
					break;
				}
			}
		}

		for ($i=$height-1; $i>=0; $i--)
		{
			if ($end_y!==null)
				break;
			for ($j=0; $j<$width; $j++)
			{
				$pos = imagecolorat($image, $j, $i).' ';
				$f = imagecolorsforindex($image, $pos);
				if ($f['red']<240 || $f['green']<240 || $f['blue']<240)
				{
					$end_y = $i;
					break;
				}
			}
		}

		for ($j=$width-1; $j>0; $j--)
		{
			if ($end_x!==null)
				break;
			for ($i=0; $i<$height; $i++)
			{
				$pos = imagecolorat($image, $j, $i).' ';
				$f = imagecolorsforindex($image, $pos);
				if ($f['red']<240 || $f['green']<240 || $f['blue']<240)
				{
					$end_x = $j;
					break;
				}
			}
		}

		$new_im = imagecreatetruecolor($end_x - $start_x, $end_y - $start_y);
		imagecopy($new_im, $image, 0, 0, $start_x, $start_y, $end_x - $start_x, $end_y - $start_y);
		return $new_im;
	}
}